import { ConfigProvider } from 'antd';
import { useEffect, useMemo, useRef, useState } from 'react';
import Pagination from '@inbiz/basic/Pagination';
import { GlobalRegistry } from '@inbiz/core';
import { getMessage, TextWidget } from '@inbiz/react';
import {getLogPagingList,getSearchLogList,editLogSwitch,getLogSwitch} from '../service'
import HeaderForm from "./HeaderForm";
import BottomTable from "./BottomTable";
import {observer} from '@formily/react'
interface IProps {
    tabskey: string;
    pagesNum: any;
}


const Log = (props: IProps) => {
  const {  tabskey, pagesNum } = props

  // 第几页
  const [page, $page] = useState(1)
  // 有几条
  const [pageSize, $pageSize] = useState(parseInt(pagesNum))
  useEffect(()=>{
    $pageSize(parseInt(pagesNum))
  },[pagesNum])

    // 列表数据总数
    const [total, $total] = useState(0)
    const [loading, $loading] = useState(false)
      // 接收表格数据
  const [tableData, $tableData] = useState()




  // 日志是否关闭
  const [logOpen, $logOpen] = useState(true)




  // 获取日志开关状态

  
  useEffect(() => {
    getLogSwitch().then((res: any) => {
      $logOpen(JSON.parse(res.Data.CfgValue))
    })
  }, []);

  // 查询

  // 搜索结果数据
  const [searchResult, $searchResult] = useState([]);

  // 获取搜索日志列表
  const getTableData = (pageIndex?: number,
        pageSize?: number, extra?: any) => {
    
        let params = {
          PageIndex: pageIndex,
          PageSize: pageSize,
          ...extra
        };
        $loading(true)
        getSearchLogList(params).then((res) => {
          let arr: any[] = []
          res.Data?.Items?.map((item: any) => {
            arr.push({ keyWord: item.keyWord, indexName: item.indexName, userName: item.userName, createTime: item.createTime, id: item.id, key: item.id, StartTime: extra?.StartTime, EndTime: extra?.EndTime })
          })
          if (extra) {
            $searchResult(extra)
          }
    
          $tableData(arr)
          $total(res.Data?.Total)
        }).finally(() => {
          $loading(false)
        })
      }
    
    // 获取问题分析日志列表
    const getAnalysisList = (pageIndex?: number,
        pageSize?: number, extra?: any) => {
        let params = {
          PageIndex: pageIndex,
          PageSize: pageSize,
          ...extra
        };
        getLogPagingList(params).then((res) => {
          let arr: any[] = []
          res.Data?.Items?.map((item: any) => {
            arr.push({ type: item.logType, url: item.url, message: item.message, createTime: item.createTime, id: item.Id, key: item.Id, detail: item.message })
          })
          
          $tableData(arr)
          $total(res.Data?.Total)
    
        })
      }

  useEffect(() => {
    if (tabskey == 'issueAnalysisLog') {
      getAnalysisList(page, pageSize,searchResult)
    } else {
      getTableData(page, pageSize, searchResult)
    }
  }, [pagesNum, tabskey, page,pageSize])

  const totalinfo = useMemo(() => {
    return getMessage('LogManagement.totalinfo')
  }, [GlobalRegistry.getDesignerLanguage()])
    return (
        <div>
  
            {/* 分页 */}
            <HeaderForm  tabskey={tabskey} $searchResult={$searchResult} getTableData={getTableData} getAnalysisList={getAnalysisList}  pageSize={pageSize} logOpen={logOpen}  ></HeaderForm>
            <BottomTable loading={loading} pagesNum={pagesNum} page={page} $page={$page} pageSize={pageSize}  getTableData={getTableData} tableData={tableData} $tableData={$tableData} tabskey={tabskey} searchResult={searchResult} $searchResult={$searchResult} total={total} getAnalysisList={getAnalysisList} logOpen={logOpen} $logOpen={$logOpen}></BottomTable>
                <Pagination
                    pageRefresh={true}
                    pagination={{
                        current: page,
                        pageSize: pageSize,
                        total: total,
                        showSizeChanger: true,
                        pageSizeOptions: [10, 20, 30, 50, 100],
                        showLessItems: true,
                        // @ts-ignore
                        showTotal: true,
                    }}
                    onPageChange={({ current, pageSize }) => {
                        $page(current);
                        $pageSize(pageSize);
                    }}
                    refreshData={() => {
                        if (tabskey == 'issueAnalysisLog') {
                            getAnalysisList(page, pageSize, searchResult);
                        } else {
                            getTableData(page, pageSize, searchResult);

                        }
                    }}
                />

        </div>
    )
}

export default observer(Log);

function typeOf(logOpen: undefined) {
  throw new Error('Function not implemented.');
}
